<?php
namespace app\admin\controller;

use app\common\model\VideoNew as VideoNewModel;
use app\common\model\VideoCate as VideoCateModel;
use app\common\model\VideoEpisode as VideoEpisodeModel;

/**
 * 寺院视频控制器
 * Class Zhvideo
 * @package app\index\controller
 */
class Videonew extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new VideoNewModel;
        $cmodel = new VideoCateModel;
        $emodel = new VideoEpisodeModel;
        $this->getPageAndSize($param);
        $map = [];
        try {
            if(!empty($param['title'])){
                $map['title'] = $param['title'];
            }
            if(!empty($param['cid'])){
                $map['cid'] = $param['cid'];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            $map['states'] = ['neq',3];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,'id,title,cid,cover,create_time,from,label,states');
            foreach ($lists as &$v){
                $cate = $cmodel->findData($v['cid'],'id','cname');
                $v['cname'] =$cate['cname'];
                $map1['vid'] = $v['id'];
                $map1['states'] = ['neq',3];
                $num = $emodel->getCount($map1);
                $v['num'] = $num;
            }
        }catch (\Exception $e) {
            writelog('查看视频列表失败'.$e->getMessage(), 0, $user);
            return admin_json(-1,$e->getMessage());
        }
        writelog('查看视频列表', 0, $user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [add  添加]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new VideoNewModel;
        $user = $this->user;
        try{
            $id = $model->addData($param);
        }catch (\Exception $e) {
            writelog('添加视频失败'.$e->getMessage(), 0, $user);
            return admin_json(-1,$e->getMessage());
        }
        writelog('查看视频成功视频id:'.$id, 0, $user);
        return admin_json(1,'添加成功');
    }
    /**
     * [edit  编辑]
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $id = $model = new VideoNewModel;
        $user = $this->user;
        try{
            $model->editData($param);
        }catch (\Exception $e) {
            writelog('编辑视频失败'.$e->getMessage(), 0, $user);
            return admin_json(-1,$e->getMessage());
        }
        writelog('编辑视频成功，视频id:'.$id, 0, $user);
        return admin_json(1,'编辑成功');
    }
    /**
     * [del  banner删除]
     * @return json
     * @author [C。]
     */
    public function del()
    {
        $param = input('param.');
        $model = new VideoNewModel;
        $user = $this->user;
        try{
            $id = $model->editData(['id'=>$param['id'],'states'=>3]);
        }catch (\Exception $e) {
            writelog('删除视频失败'.$e->getMessage(), 0, $user);
            return admin_json(-1,$e->getMessage());
        }
        writelog('删除视频成功，视频id:'.$id, 0, $user);
        return admin_json(1,'删除成功');
    }
}